package Ctr;

import DB.*;
import Model.*;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


public class AttendanceCtr implements AttendanceCtrIF {

    private SessionCtr sessionCtr;

    public AttendanceCtr() {
        sessionCtr = new SessionCtr();
    }

    /**
     * Get course attendance by teacher and current date
     * @param teacher
     * @param date
     * @return list of attendances for the current date
     */
    @Override
    public ArrayList<Attendance> getCourseAttendanceByTeacherIDAndCurrentDate(Teacher teacher, String date) {
        SessionDB sessionDB = new SessionDB();
        AttendanceDB attendanceDB = new AttendanceDB();

        Session session = sessionDB.getSessionByTeacherAndDate(teacher, date);
        ArrayList<Attendance> attendanceList = attendanceDB.getAllAttendancesBySessionID(session);

        return attendanceList;
    }

    /**
     * Get all attendances for one student between dates
     * @param student
     * @param endDate
     * @return total attendance in percent
     */
    @Override
    public String getAllAttendanceForOneStudentByIDAndDate(Student student,String endDate) {
        String startDate = student.getTeamId().getSemester().getStartDate();

        AttendanceViewDB attendanceViewDB = new AttendanceViewDB();

        ArrayList<AttendanceView> attendanceViewsList =
                attendanceViewDB.getAllAttendanceViewForOneStudentByIdAndBetweenDates(false, student.getId(),
                        startDate, endDate);

        int sessionAmount = 0;
        int attendanceAmount = 0;

        for (AttendanceView a : attendanceViewsList) {
            if (a.isAttending() == true) {
                attendanceAmount = attendanceAmount + 1;
            }
            sessionAmount = sessionAmount + 1;
        }

        NumberFormat formatter = new DecimalFormat("#0.00");
        double totalAttendance = 0;

        if (sessionAmount != 0) {
            totalAttendance = ((double)attendanceAmount * 100) / (double)sessionAmount;
            return formatter.format(totalAttendance);
        }
        else {
            return formatter.format(totalAttendance);
        }

    }

    /**
     * Get all active course for one student
     * @param student
     * @return list of courses
     */
    @Override
    public ArrayList<Course> getAllCoursesForOneStudent(Student student)
    {   AttendanceViewDB attendanceViewDB = new AttendanceViewDB();
        CourseDB courseDB = new CourseDB();
        ArrayList<Course> courses = new ArrayList<Course>();
        ArrayList<Integer> courseIds = attendanceViewDB.getAllDistinctCoursesByStudentId(false,student.getId());
        for(int courseId: courseIds)
        {
            courses.add(courseDB.findCourse(courseId));
        }

        return courses;
    }

    /**
     * Get the session by teacher and date
     * @param teacher
     * @param date
     * @return session object
     */
    @Override
    public Session getSessionByTeacherAndDate(Teacher teacher, String date) {
        Session session = sessionCtr.getSessionByTeacherAndDate(teacher, date);
        return session;
    }

    /**
     * Get course attendances for one student between dates
     * @param student
     * @param courseId
     * @param endDate
     * @return course attendance in percent
     */
    @Override
    public String getCourseAttendanceByIDAndDate(Student student, int courseId, String endDate) {
        String startDate = student.getTeamId().getSemester().getStartDate();

        AttendanceViewDB attendanceViewDB = new AttendanceViewDB();
        ArrayList<AttendanceView> attendanceViewsList = attendanceViewDB.getAllAttendanceViewByStudentAndCourseBetweenDates(false, student.getId(), courseId, startDate, endDate);

        //Initializing variables
        int courseAmount = 0;
        int attendanceAmount = 0;

        //For each Session in sessionList
        for (AttendanceView a : attendanceViewsList) {
            //If attendance isAttending is true and attendance(courseId) = courseId
            if (a.isAttending() == true) {
                attendanceAmount = attendanceAmount + 1;
            }
            courseAmount = courseAmount + 1;
        }

        NumberFormat formatter = new DecimalFormat("#0.00");
        double totalAttendance = 0;

        if (courseAmount != 0) {
            totalAttendance = ((double)attendanceAmount * 100) / (double)courseAmount;
            return formatter.format(totalAttendance);
        }
        else {
            return formatter.format(totalAttendance);
        }
    }

    /**
     * Student check-in
     * @param student
     * @param date
     * @return true/false
     */
    @Override
    public boolean studentCheckIn(Student student, String date) {
        AttendanceDB attendanceDB = new AttendanceDB();
        SessionDB sessionDB = new SessionDB();

        Session session = sessionDB.findSessionByStudentIdAndDate(student, date);

        Attendance attendance = attendanceDB.getAttendanceByStudentAndSessionId(student, session);

        if (attendance == null) {
            return false;
        }
        else {
            attendance.setIsAttending(true);
            attendanceDB.updateAttendance(attendance);
            return true;
        }
    }

    /**
     * Change student attendance
     * @param studentNo
     * @param session
     */
    @Override
    public void changeAttendance(int studentNo, Session session) {
        AttendanceDB attendanceDB = new AttendanceDB();
        StudentDB studentDB = new StudentDB();
        Student student = studentDB.findStudentByStudentNo(studentNo);

        Attendance attendance = attendanceDB.getAttendanceByStudentAndSessionId(student, session);

        attendance.setIsAttending(!attendance.getIsAttending());

        attendanceDB.updateAttendance(attendance);
    }

    /**
     * Change student attendance comment
     * @param studentNo
     * @param session
     * @param comment
     */
    @Override
    public void changeComment(int studentNo, Session session, String comment) {
        AttendanceDB attendanceDB = new AttendanceDB();
        StudentDB studentDB = new StudentDB();
        Student student = studentDB.findStudentByStudentNo(studentNo);

        Attendance attendance = attendanceDB.getAttendanceByStudentAndSessionId(student, session);

        attendance.setComment(comment);

        attendanceDB.updateAttendance(attendance);
    }

    /**
     * Gets the current date
     * @return current date
     */
    @Override
    public String getCurrentDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return dateFormat.format(date);
    }
}
